<template>
  <div class="os-main">
    <ConfigForm v-model="dataForm" :config="config" ref="configFormRef" />

    <el-button type="primary" @click="openJson">查看表单数据</el-button>

    <el-drawer v-model="drawerVisiable" title="数据视图" :direction="'rtl'" size="40%">
      <CodeView :code-json="JSON.stringify(dataForm, null, '\t')"></CodeView>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup>
import type { Config } from '@/components/ConfigForm'
import { ElRadio, ElRadioButton } from 'element-plus';
import { ref, h } from 'vue';
import CodeView from '@/components/CodeView/index.vue'
import ConfigForm from '@/components/ConfigForm/index'


const radioOptions = [
  { label: '上海', value: 'Shanghai' },
  { label: '北京', value: 'Beijing' },
  { label: '广州', value: 'Guangzhou', disabled: true },
  { label: '深圳', value: 'Shenzhen' }
]

/**ConfigForm绑定对象 */
const dataForm = ref<ObjectAny>({})

/**ConfigForm配置 */
const config = ref<Config>({
  mode: 'add',
  formProps: {},
  rowProps: {},
  columns: [
    { // 基础用法
      prop: 'demo1', label: '基础用法', type: 'radioGroup',
      events: {
        change: (val: string) => { // 通过选框label获取真实的值
          const target = radioOptions.find(o => o.label === val)
          dataForm.value.realDemo1 = target?.value
        }
      },
      slots: { default: h('div', {}, radioOptions.map(item => h(ElRadio, item, ''))) }
    },
    { // 按钮样式
      prop: 'demo2', label: '按钮样式', type: 'radioGroup',
      events: {
        change: (val: string) => { // 通过选框label获取真实的值
          const target = radioOptions.find(o => o.label === val)
          dataForm.value.realDemo2 = target?.value
        }
      },
      slots: {
        default: h('div', { style: { width: '100%' } }, radioOptions.map(item =>
          h(ElRadioButton, { ...item, style: { width: 100 / radioOptions.length + '%' } }, '')
        ))
      }
    },
    { // 带有边框
      prop: 'demo3', label: '带有边框', type: 'radioGroup',
      events: {
        change: (val: string) => { // 通过选框label获取真实的值
          const target = radioOptions.find(o => o.label === val)
          dataForm.value.realDemo3 = target?.value
        }
      },
      slots: { default: h('div', {}, radioOptions.map(item => h(ElRadio, { ...item, border: true }, ''))) }
    },
  ]
})

// 数据视图
const drawerVisiable = ref(false)
const openJson = () => {
  drawerVisiable.value = true
}
</script>
<style lang="scss" scoped>
:deep(.el-radio-button__inner) {
  width: 100%;
}
</style>